// pages/photo/detail/detail.js
const http = require("../../../utils/request");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    enable: false,
    loadingProps: {
      size: '40rpx',
    },
    backTopVisible: false,
    isEdit: false,
    datas: {},
    p_arr: [],
    type: {},
    iconList: [],
    pic: [],
    photo: {},
    // showImgsIndex:null //用于image传入数据
  },
  /**
   * 绑定事件的函数
   */
  /**
   * @public 触碰img
   */
  tapImage(event) {
    console.log(event)
    this.setData({
      showImgsIndex: event.currentTarget.dataset.index
    })
  },
  //卡片
  tapCard(event) {
    if (this.data.isEdit) {
      let iconList = this.data.iconList
      if (iconList[event.currentTarget.dataset.index] == 'check-circle-filled') {
        iconList[event.currentTarget.dataset.index] = 'circle'
      } else {
        iconList[event.currentTarget.dataset.index] = 'check-circle-filled'
      }
      this.setData({
        iconList: iconList
      })
    }
  },
  // 底部按钮
  tapCancelOrAdd() {
    if (this.data.isEdit) {// 确定
      console.log('选中', this.countInArr(this.data.iconList, 'check-circle-filled'))
      this.setData({
        isEdit: !this.data.isEdit,
        iconList: this.data.iconList.fill('move')
      })
    } else {// 新增
      this.gotoAddVerification()
    }
  },
  tapComfirmOrEdit() {
    if (this.data.isEdit) {// 取消
      this.setData({
        iconList: this.data.iconList.fill('move')
      })
    } else {// 编辑
      this.setData({
        iconList: this.data.iconList.fill('circle')
      })
    }
    this.setData({
      isEdit: !this.data.isEdit
    })
  },
  //滑动
  onEdit(event) {
    console.log(this.data.arr_p[event.currentTarget.dataset.index])

    console.log(event.currentTarget.dataset.index)
    this.gotoAddVerification(this.data.arr_p[event.currentTarget.dataset.index])
    // this.gotoAddVerification()
  },

  /**
  * 路由跳转
  */
  //传数据则为修改否则为新增
  gotoAddVerification(data) {
    data = data ? JSON.stringify(data) : {}
    wx.navigateTo({
      url: '../addVerification/addVerification?data=' + data,
      events: false,
      success: (result) => { },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  /**
  * 通用函数
  */
  countInArr(array, element) {
    return array.map((_, index) => index).filter(i => array[i] === element);
  },
  load_arr() {
    console.log(this.data.datas);
    http.getRequest("/admin/verification/list/" + this.data.type.type + "/" + this.data.datas.code, {}, res => {
      this.setData({
        arr_p: res.data.data.verificationList,
      })
    }, err => { });
  },
  onDelete(e) {
    console.log(e);
    let id = e.currentTarget.dataset.data.id;
    http.delRequest("/admin/verification/" + id, {}, res => {
      if (res.data.success) {
        wx: wx.showToast({
          title: '删除成功',
        })
        this.load_arr();
      } else {
        wx: wx.showToast({
          title: '删除失败',
          icon: 'error',
        })
      }
    }, err => { });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let app = getApp().globalData;
    let datas = app.photo_cultivation;
    let type = app.photo_type;
    let photo = app.photo;
    console.log(photo);
    let iconList = new Array(6).fill('move');
    this.setData({
      datas,
      type,
      photo,
      iconList
    })
    this.load_arr();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.load_arr();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onRefresh() {
    this.setData({ enable: true });
    this.onLoad();
    setTimeout(() => {
      this.setData({ enable: false });
    }, 500);
  },
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({
      backTopVisible: scrollTop > 100,
    });
  },
})